// pages/Index/Customer/UserDeatils.js
const controllers = require('../../controllers/Controller.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:[
      {
        name:"普通用户",
        id:0
      },
      {
        name:"普通会员",
        id:2
      },
      {
        name:"VIP会员",
        id:1
      }
    ],
    value:{
      shopId:"",
      remarks:"",
      sex:"",
      birthday:"",
      desc:"",
      accountRelationId:"",
    },
    isSave:true,
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831918901884354560.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831919079068532736.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this
    this.setData({
      'value.shopId':options.shopId
    })
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:options.shopId,
      accountRelationId:options.id
    }
    this.setData({
      params:params
    })
    controllers.accountDetail(
      params
    ).then(res =>{
      let vipList=[]
        if(res.data.vipList&&res.data.vipList.length>0){
          res.data.vipList.forEach(element => {
            let p =0
            if(element.endTime){
            let endTime =that.getDateTime(element.endTime)
            let createTime=that.getDateTime(element.createTime)
           let total= endTime-createTime
           let time = endTime-new Date().getTime(); 
             p =time/total;
           }
           element.p=p*100
           vipList.push(element)
          });
        }
    
      that.setData({
        detail:res.data,
        vipList,
        "value.accountRelationId":res.data.id||'',
        "value.remarks":res.data.remarks||'',
        "value.sex":res.data.sex||"",
        "value.birthday":res.data.birthday||'',
        "value.desc":res.data.desc||''
      })
      if(res.data.remarks){
        this.setData({
          isSave:false
        })
      }else{
        this.setData({
          isSave:true
        })
      }
    })
  },
  //获取输入用户数据
  getValue(e){
    let type=e.currentTarget.dataset.type
    let value=e.detail.value
    console.log(e)
    if(type=="remarks"){
      this.setData({
        'value.remarks':value
      })
    }else if(type=="sex"){
      this.setData({
        'value.sex':value
      })
    }else if(type=="birthday"){
      this.setData({
        'value.birthday':value
      })
    }else if(type=="desc"){
      this.setData({
        'value.desc':value
      })
    }
  },

  //跳转线下开单
  toCreateOrder(){
    let accountRelationId=this.data.detail.id
    let accountNick=this.data.detail.accountNick
    let accountMobile=this.data.detail.accountMobile
    wx.navigateTo({
      url: '/pages/settlement/Offline/Offline?accountRelationId='+accountRelationId+"&accountNick="+accountNick+"&accountMobile="+accountMobile,
    })
  },

  //跳转线下开卡
  toCreateCard(){
    let accountId=this.data.detail.accountId
    let accountNick=this.data.detail.accountNick
    let accountMobile=this.data.detail.accountMobile
    wx.navigateTo({
      url: '/pages/settlement/OpenCard/OpenCard?accountId='+accountId+"&accountNick="+accountNick+"&accountMobile="+accountMobile,
    })
  },

  //备注用户信息
  subPersonMsg(){
    // if(!this.data.value.remarks){
    //   wx.showToast({
    //     icon:"none",
    //     title: '请输入姓名',
    //     duration:1500
    //   })
    // }else if(!this.data.value.sex){
    //   wx.showToast({
    //     icon:"none",
    //     title: '请输入性别',
    //     duration:1500
    //   })
    // }else if(!this.data.value.birthday){
    //   wx.showToast({
    //     icon:"none",
    //     title: '请输入生日',
    //     duration:1500
    //   })
    // }else if(!this.data.value.desc){
    //   wx.showToast({
    //     icon:"none",
    //     title: '请输入信息备注',
    //     duration:1500
    //   })
    // }else{
      // controllers.setAccountDesc(this.data.value).then(res=>{
      //   wx.showToast({
      //     icon:"none",
      //     title: '备注成功',
      //     duration:1500
      //   })
      // })
    // }
  },

  getDateTime(date){
   
    date= date.replace(new RegExp("-","gm"),"/");
    var time = (new Date(date)).getTime(); 
    return time
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }


  //选择会员等级
  // bindPickerChange(e){
  //   let that=this
  //   let value={}
  //   let params=this.data.params
  //   value.vip=this.data.array[e.detail.value].id
  //   value.shopId=wx.getStorageSync('shopId') 
  //   value.accountId=this.data.detail.accountId
  //   controllers.updataVip(value).then(res=>{
  //     wx.showToast({
  //       icon:"none",
  //       title:"修改成功",
  //       duration:1500
  //     })
  //     controllers.accountDetail(
  //       params
  //     ).then(res =>{
  //       let vipList=[]
  //         if(res.data.vipList&&res.data.vipList.length>0){
  //           res.data.vipList.forEach(element => {
  //             let p =0
  //             if(element.endTime){
  //             let endTime =that.getDateTime(element.endTime)
  //             let createTime=that.getDateTime(element.createTime)
  //            let total= endTime-createTime
  //            let time = endTime-new Date().getTime(); 
  //              p =time/total;
  //            }
  //            element.p=p*100
  //            vipList.push(element)
  //           });
  //         }
      
  //       that.setData({
  //         detail:res.data,
  //         vipList
  //       })
  //     })
  //   })

  // }

  //跳转订单列表页
  toWatchOrder(){
    wx.navigateTo({
      url: '/pages/Index/Customer/orderTypeList?accountId='+this.data.detail.accountId+"&shopId="+this.data.detail.shopId,
    })
  },

  //跳转项目详情
  toWriteOff(){
    wx.navigateTo({
      url: '/pages/Index/Customer/writeOff?accountId='+this.data.detail.accountId+"&shopId="+this.data.detail.shopId,
    })
  },
})